package myfirstproject.firstproject.Controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import myfirstproject.firstproject.Service.IUserService;
import myfirstproject.firstproject.Util.JwtUtil;
import myfirstproject.firstproject.constant.JwtClaimsConstant;
import myfirstproject.firstproject.domain.DTO.UserDTO;
import myfirstproject.firstproject.domain.DTO.UserLoginDTO;
import myfirstproject.firstproject.domain.DTO.UserQueryDTO;
import myfirstproject.firstproject.domain.VO.UserLoginVO;
import myfirstproject.firstproject.domain.VO.UserVo;
import myfirstproject.firstproject.domain.po.User;
import myfirstproject.firstproject.properties.JwtProperties;
import myfirstproject.firstproject.result.PageResult;
import myfirstproject.firstproject.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

import static com.baomidou.mybatisplus.core.toolkit.Wrappers.lambdaQuery;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author author
 * @since 2024-09-16
 */
@RestController
@RequestMapping("/user")
@Tag(name = "用户管理")
@RequiredArgsConstructor
@Slf4j
public class UserController {
    @Autowired
    private IUserService userService;
    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 登录
     * @param userDTO
     * @return
     */
    @Operation(summary = "登录")
    @PostMapping("/login")
    public Result<UserLoginVO> login(@RequestBody UserLoginDTO userDTO) {
        log.info("用户登录:{}", userDTO);
        User user =userService.login(userDTO);
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.USER_ID, user.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);
        UserLoginVO userLoginVO = UserLoginVO.builder()
                .id(user.getId())
                .username(user.getUsername())
                .nickname(user.getNickname())
                .token(token)
                .build();
        return Result.success(userLoginVO);


    }

    /**
     * 新增用户
     * @param userDTO
     * @return
     */
    @Operation(summary = "新增用户接口")
    @PostMapping("/save")
    public Result saveUser(@RequestBody UserDTO userDTO) {
        //新增用户
        userService.saveUser(userDTO);
        return Result.success();
    }
    /**
     * 删除用户
     * @param id
     * @return
     */
    @Operation(summary = "删除用户")
    @DeleteMapping("/delete/{id}")
    public Result deleteUser(@PathVariable String id) {
        userService.removeById(id);
        return Result.success();
    }

    /**
     * 修改用户
     * @param userDTO
     * @return
     */
    @Operation(summary = "修改用户")
    @PostMapping("/update")
    public Result updateUser(@RequestBody UserDTO userDTO) {
        userService.updateUser(userDTO);
        return Result.success();
    }

    /**
     * 根据id查询用户
     * @param id
     * @return
     */
    @Operation(summary = "查询用户")
    @GetMapping("/query/{id}")
    public UserVo queryUser(@ApiParam("用户id") @PathVariable("id") String id) {
        return userService.queryUserAndAddressById(id);
    }

    @Operation(summary = "分页查询用户")
    @PostMapping("/page")
    public PageResult<UserVo> queryUsersPage(@RequestBody UserQueryDTO query){
        return userService.queryUsersPage(query);
    }




}
